package com.situ.web.dao;

import java.util.List;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.situ.web.entity.Teacher;
import com.situ.web.util.JDBCUtil;

public class TeacherDao {

	//查询数据
	public List<Teacher> findAll() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		ArrayList<Teacher> list = new ArrayList<Teacher>();
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,age,gender from teacher";
			// 得到prepareStatement而且把sql语句传过去
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");

				Teacher teacher = new Teacher(id, name, age, gender);
				list.add(teacher);
			}
			

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		
		return list;
	}
	
	//删除
	public int deleteById(int id) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
 		try {
			connection = JDBCUtil.getConnection();
			String sql = "Delete FROM teacher WHERE id = ?"; 
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, id);
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("删除成功");
			} else {
				System.out.println("删除失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		
		return count;
		
	}
	
	//添加老师
	public int insertTeacher(Teacher teacher) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "INSERT INTO teacher(id,`name`,age,gender) VALUES(?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, teacher.getId());
			preparedStatement.setString(2, teacher.getName());
			preparedStatement.setInt(3, teacher.getAge());
			preparedStatement.setString(4, teacher.getGender());
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("插入成功");
			} else {
				System.out.println("插入失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
		
	}
	
	//修改1：查询ID
	public Teacher SelectById(int Selectid) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		Teacher teacher = null;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "select id,name,age,gender from teacher where id = ?";
			// 得到prepareStatement而且把sql语句传过去
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setInt(1, Selectid);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				int id = resultSet.getInt("id");
				String name = resultSet.getString("name");
				int age = resultSet.getInt("age");
				String gender = resultSet.getString("gender");

				teacher = new Teacher(id, name, age, gender);
				
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, resultSet);
		}
		return teacher;
	}
	
	//修改2，修改操作
	public int updateTeacher(Teacher teacher) {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		int count = 0;
		try {
			connection = JDBCUtil.getConnection();
			String sql = "UPDATE teacher SET `name` = ? , age = ? , gender = ? WHERE id= ?;";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, teacher.getName());
			preparedStatement.setInt(2, teacher.getAge());
			preparedStatement.setString(3, teacher.getGender());
			preparedStatement.setInt(4, teacher.getId());
			 
			count = preparedStatement.executeUpdate();
			if (count == 1) {
				System.out.println("修改成功");
			} else {
				System.out.println("修改失败");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
		return count;
		
	}
}
